package com.huayun.hyweex.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;

import com.huayun.activity.WebActivity;
import com.huayun.base.BaseActivity;
import com.huayun.activity.ScanActivity;
import com.huayun.data.BaseConstant;
import com.huayun.hyweex.R;
import com.huayun.util.utils.LogUtil;
import com.huayun.util.utils.SharedPreferenceUtil;
import com.huayun.weexutil.WeexActivity;

import butterknife.BindView;

public class MainActivity extends BaseActivity {
    @BindView(R.id.et)
    protected EditText editText;
    private int requestCode = 0x1111;

    @Override
    protected void bindView() {
        editText.setText("file://assets/js/app.js");
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    protected int getContentViewLayoutResId() {
        return R.layout.activity_main;
    }

    public void input(View view) {
        SharedPreferenceUtil.put("prefix_weex_url", editText.getText().toString());
        String prefix = (String) SharedPreferenceUtil.get("prefix_weex_url", "");
        startWeexActivity(prefix);
    }

    public void scan(View view) {
        startActivityForResult(new Intent(this, ScanActivity.class), requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == this.requestCode && resultCode == BaseConstant.RESULT_SCAN_URL_CODE) {
            if (data != null) {
                String back = data.getStringExtra(BaseConstant.SCAN_URL_DATA);
                LogUtil.e(back);
                if (back.contains("html")) {
                    Intent intent = new Intent(MainActivity.this, WebActivity.class);
                    intent.putExtra("webUrl",back);
                    intent.putExtra("webTitle","");
                    startActivity(intent);
                } else {
                    startWeexActivity(back);
                }

            }
        }
    }

    public void startWeexActivity(String url) {
        String[] split = url.split("\\?");
        SharedPreferenceUtil.put("prefix_weex_url", split[0].substring(0, split[0].lastIndexOf("/") + 1));
        Intent intent = new Intent(this, WeexActivity.class);
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        startActivity(intent);
    }
}
